<?php
class User extends AppModel
{

    public $name = 'User';

    //The Associations below have been created with all possible keys, those that are not needed can be removed
    public $hasMany = array(
        'Order'             => array('className' => 'Order',
                                        'foreignKey' => 'user_id',
                                        'dependent' => true,
                                        'conditions' => '',
                                        'fields' => '',
                                        'order' => array('created'=>'desc'),
                                        'limit' => '',
                                        'offset' => '',
                                        'exclusive' => '',
                                        'finderQuery' => '',
                                        'counterQuery' => ''
        )

    );

    public $hasAndBelongsToMany = array(
        'Group'             => array('className' => 'Group',
                                        'joinTable' => 'groups_users',
                                        'foreignKey' => 'user_id',
                                        'associationForeignKey' => 'group_id',
                                        'unique' => true,
                                        'conditions' => '',
                                        'fields' => '',
                                        'order' => '',
                                        'limit' => '',
                                        'offset' => '',
                                        'finderQuery' => '',
                                        'deleteQuery' => '',
                                        'insertQuery' => ''
        )
    );

    public $hasOne = array(
        'TransportAddress'  => array('className' => 'TransportAddress',
                                        'foreignKey' => 'user_id',
                                        'dependent' => true,
                                        'conditions' => array('TransportAddress.type'=>'transport'),
                                        'fields' => '',
                                        'order' => '',
                                        'limit' => '',
                                        'offset' => '',
                                        'exclusive' => '',
                                        'finderQuery' => '',
                                        'counterQuery' => ''
        ),

        'InvoiceAddress'    => array('className' => 'InvoiceAddress',
                                        'foreignKey' => 'user_id',
                                        'dependent' => true,
                                        'conditions' => array('InvoiceAddress.type'=>'invoice'),
                                        'fields' => '',
                                        'order' => '',
                                        'limit' => '',
                                        'offset' => '',
                                        'exclusive' => '',
                                        'finderQuery' => '',
                                        'counterQuery' => ''
        ),
    );

    public $validate = array(
        'email' => array(
            'required' => array(
                'rule' => 'notEmpty',
                'message' => 'This field cannot be left blank'
            ),
            'format' => array(
                'rule' => array('email', false),
                'message' => 'Please supply a valid email address.'
            ),
        ),
        'password' => array(
            'required' => array(
                'rule' => 'notEmpty',
                'message' => 'This field cannot be left blank'
            ),
        ),
        'name' => array(
              'required' => array(
                'rule' => 'notEmpty',
                'message' => 'This field cannot be left blank'
            ),
        ),
        'company' => array(
              'required' => array(
                'rule' => 'notEmpty',
                'message' => 'This field cannot be left blank'
            ),
        ),
        'phone' => array(
            'required' => array(
                'rule' => 'notEmpty',
                'message' => 'This field cannot be left blank'
            ),
        ),

    );

    public function getGroups($userId = null) {
        if (empty($userId) && empty($this->id)) {
            return array();
        }

        if (!empty($userId)) {
            $this->id = $userId;
        }

        $this->recursive = 1;
        $user = $this->read("",$this->id);

        return (!$user) ? array() : $user['Group'];
    }

}
?>